//
//  LRNewfeatureController.swift
//  LRWeibo
//
//  Created by 雷路荣 on 15/12/17.
//  Copyright © 2015年 leilurong. All rights reserved.
//

import UIKit

private let reuseIdentifier = "LRNewfeatureCell"

class LRNewfeatureController: UICollectionViewController {
    //自动提示产生
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //流水布局
    private let layout = UICollectionViewFlowLayout()
    // AppDelegate会调用没有参数的构造函数
    init(){
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        // 调用父类的需要layout的构造函数
        super.init(collectionViewLayout: layout)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //分页滑动
        self.collectionView?.pagingEnabled = true
        //去掉弹簧效果
        self.collectionView?.bounces = false
        self.collectionView?.showsHorizontalScrollIndicator = false
        // 代码创建cell注册方式  LRNewfeatureCell.self相当于OC [LRNewfeatureCell self]
        self.collectionView!.registerClass(LRNewfeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    let itemCount = 4
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // 去缓存池中查找
        // as 类型转换, 但是有可能转成功,也有可能转失败
        // as! 表示一定能转成功,如果一旦转换失败,程序崩溃
        // as? 表示有可能转成功,也有可能转不成功, 返回值是一个可选
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! LRNewfeatureCell
        //切换图片，隐藏立即体验按钮
        cell.imageIndex = indexPath.item
        // 在这里动画,一显示cell的时候就会动画,我们想要停下来时再动画
//        if indexPath.item==ItemCount-1 {
//            cell.startButtonAnimation()
//        }
        return cell
    }
    // collectionView显示完毕时调用(在屏幕上看不到的时候会调用)----collectionView停到某一页
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //print(__FUNCTION__)
        //print("indexPath:\(indexPath.item)")//貌似没规律
        // collectionView停到某一页,不使用indexPath,我们自己来获取现在正在显示的cell的indexPath
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        print("visibleIndexPath.item:\(visibleIndexPath.item)")
        // 判断是否是最后一个
        if visibleIndexPath.item == itemCount-1 {
            // 获取visibleIndexPath对应的cell
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath) as! LRNewfeatureCell
            //动画显示按钮
            cell.startButtonAnimation()
        }
    }
}





//--------------------------------------自定义cell--------------------------------\\
//MARK:-自定义cell显示新特性界面
class LRNewfeatureCell :UICollectionViewCell {
    // MARK: - 属性 图片索引
    var imageIndex = 0 {
        didSet{// 属性监视器, 当imageIndex发生变化时会调用
            //隐藏按钮
            startButton.hidden = true
            //设置cell图片
            self.bgImageView.image = UIImage(named: "new_feature_\(imageIndex+1)")
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 重写系统方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    // MARK: - 添加子控件
    private func prepareUI(){
        contentView.addSubview(bgImageView)
        contentView.addSubview(startButton)
    }
    // MARK: - 布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()
        bgImageView.frame = self.bounds
        startButton.center = CGPointMake(self.bounds.size.width*0.5, self.bounds.size.height*0.8)
    }
     // MARK: - 懒加载, 按钮会自动适配图片的大小
    lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    // MARK: - 懒加载, 开始按钮
    lazy var startButton: UIButton = {
        let button = UIButton()
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        //图片标题自动适应大小
        button.sizeToFit()
        //因为cell复用,不能创建的时候来隐藏button,往回推动界面不该有按钮的界面会出现按钮
        //button.hidden = true
        button.addTarget(self, action: "startButtonDidClick", forControlEvents: UIControlEvents.TouchDown)
        return button
    }()
    //立即体验按钮点击事件
    @objc private func startButtonDidClick() {
        AppDelegate.outSwitchRootViewController(true)
    }
    
    // MARK: - 开启开始体验按钮显示动画
    func startButtonAnimation() {
        //显示按钮
        startButton.hidden = false
        //先缩小按钮为0
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        //开启弹簧动画
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //还原按钮大小
            self.startButton.transform = CGAffineTransformIdentity//CGAffineTransformMakeScale(1, 1)
            }) { (_) -> Void in
            print("动画显示完毕")
        }
    }
}





































